<template>
  <div>
    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <el-form :model="formData">
        <el-form-item label="用户头像:" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
            :http-request="Upload"
          >
            <img v-if="formData.avatar" :src="formData.avatar" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="用户账号:" :label-width="formLabelWidth">
          <el-input
            style="width: 80%"
            size="mini"
            v-model="formData.username"
            placeholder="请输入用户账号"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户名称:" :label-width="formLabelWidth">
          <el-input
            style="width: 80%"
            size="mini"
            v-model="formData.name"
            placeholder="请输入用户名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户性别:" :label-width="formLabelWidth">
          <el-select
            size="mini"
            v-model="formData.sex"
            placeholder="请选择用户性别"
            clearable
          >
            <el-option
              v-for="(item, index) in options"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户地址:" :label-width="formLabelWidth">
          <el-input
            style="width: 80%"
            size="mini"
            v-model="formData.address"
            placeholder="请输入用户地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="个性签名:" :label-width="formLabelWidth">
          <el-input
            type="textarea"
            style="width: 80%"
            size="mini"
            v-model="formData.sign"
            placeholder="请输入个性签名"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { updateInfo } from "../../../api/nested";
import axios from "axios";
export default {
  data() {
    return {
      dialogFormVisible: false,
      formData: {
        username: "",
        name: "",
        sex: "",
        address: "",
        sign: "",
        avatar: "",
      },
      fileData: {},
      uploadUrl: "",
      formLabelWidth: "150px",
      options: [
        {
          label: "男",
          value: "男",
        },
        {
          label: "女",
          value: "女",
        },
      ],
    };
  },
  methods: {
    open(row) {
      this.formData = row;
      this.formData.avatar = "";
      this.dialogFormVisible = true;
    },
    //自定义上传请求
    async Upload() {
      const response = await axios.post(
        "http://127.0.0.1:3007/upLoad/file",
        this.fileData,
        {
          headers: {
            "Content-Type": "multipart/form-data",
          },
        }
      );
      console.log(response.data.url);
      this.formData.avatar = response.data.url.replace(/\\/g, "/");
      console.log(this.formData.avatar);
      
    },
    //上传成功
    handleAvatarSuccess(res, file) {
     
    },
    //上传之前
    beforeAvatarUpload(file) {
      console.log(file);
      this.fileData = new FormData();
      // 将文件添加到 FormData 中
      this.fileData.append("file", file);

      //   this.handleAvatarSuccess(response.data, file, []);
      // const isJPG = file.type === 'image/jpeg';
      // const isLt2M = file.size / 1024 / 1024 < 2;

      // // if (!isJPG) {
      // //     this.$message.error('上传头像图片只能是 JPG 格式!');
      // // }
      // // if (!isLt2M) {
      // //     this.$message.error('上传头像图片大小不能超过 2MB!');
      // // }
      // return isJPG && isLt2M;
    },
    async confirm() {
      const res = await updateInfo(this.formData);
      if (res.status != 0) {
        this.$message.error(res.message);
        return;
      }
      this.$emit("update", "success");
      this.dialogFormVisible = false;
    },
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}

.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
